package com.eascs.web.o2o.scheduler.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.web.o2o.dc.dao.DcDao;
import com.eascs.web.o2o.dc.dao.OaDao;
import com.eascs.web.o2o.dc.dao.ServiceDao;
import com.eascs.web.o2o.dc.dao.WorkFlowDao;
import com.eascs.web.o2o.report.entity.HrEmployee;
import com.eascs.web.o2o.scheduler.entity.HrEmployeeAuditor;
import com.eascs.web.o2o.scheduler.entity.HrOrgan;
import com.eascs.web.o2o.scheduler.entity.O2oOverdueReport;
import com.eascs.web.o2o.scheduler.entity.OaOverDueReport;
import com.eascs.web.o2o.scheduler.util.Constant;
import com.eascs.workflow.common.entity.sys.ActOrgUser;

@Service
public class OaSynchroService {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	private DcDao dcDao;
	@Inject
	private WorkFlowDao workFlowDao;
	@Inject
	private OaDao oaDao;
	@Inject
	private ServiceDao serviceDao;

	/**
	 * 修改为增量同步员工审核人表
	 */
	public void synchroEmployeeAuditor() {
		logger.info("开始同步员工审核表");
		String employeeSql = "SELECT * FROM act_org_user t where t.is_remove='0' GROUP BY t.user_no";
		int index = 0;
		int count = 0;
		while (index >= 0) {
			SqlPageQuery<ActOrgUser> sqlPageQuery = new SqlPageQuery<ActOrgUser>(ActOrgUser.class, Constant.PROCESS_DATA_LIMIT, index, employeeSql);
			PageResult<ActOrgUser> pageResult = workFlowDao.findDataWithPage(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				ActOrgUser actOrgUser = pageResult.getData().get(i);
				String oaSql = "select * from eadb.hr_employeeauditor r where  r.employeeno=?";
				List<HrEmployeeAuditor> employeeAuditorList = oaDao.findEntitys(oaSql, HrEmployeeAuditor.class, actOrgUser.getUserNo());
				if (employeeAuditorList != null && employeeAuditorList.size() > 0) {
					for (HrEmployeeAuditor hrEmployeeAuditor : employeeAuditorList) {
						String querySql="select * from hr_employeeauditor t  where t.EMPLOYEEAUDITORNO=?";
						try {
							HrEmployeeAuditor hrEmployee=dcDao.findEntity(querySql, HrEmployeeAuditor.class, hrEmployeeAuditor.getEmployeeauditorno());
							if(hrEmployee!=null){
								dcDao.updateById(hrEmployeeAuditor);
								logger.info("修改数据中心HrEmployeeAuditor:[{}]", hrEmployeeAuditor.toString());
								count++;
							}else{
								dcDao.saveEntity(hrEmployeeAuditor);
								logger.info("插入数据中心HrEmployeeAuditor:[{}]", hrEmployeeAuditor.toString());
								count++;
							}
						} catch (Exception e) {
							logger.info("同步员工审核表到数据中心异常：[{}]", e.getMessage());
						}
						try {
							HrEmployeeAuditor hrEmployee=dcDao.findEntity(querySql, HrEmployeeAuditor.class, hrEmployeeAuditor.getEmployeeauditorno());
							if(hrEmployee!=null){
								workFlowDao.updateById(hrEmployeeAuditor);
								logger.info("修改工作流HrEmployeeAuditor:[{}]", hrEmployeeAuditor.toString());
							}else{
								workFlowDao.insert(hrEmployeeAuditor);
								logger.info("修改工作流HrEmployeeAuditor:[{}]", hrEmployeeAuditor.toString());
							}
						} catch (Exception e) {
							logger.info("同步员工审核表到工作流异常：[{}]", e.getMessage());
						}
					}
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("结束同步员工审核表条数:[{}]", count);
	}

	/**
	 * 同步老系统逾期数据 只需要初始化一次
	 */
	public void synchroOverdueReport() {
		String sql = "select * from eadb.O2O_OVERDUE_REPORT t  where t.isdel='N' ";
		int index = 0;
		int count = 0;
		while (index >= 0) {
			SqlPageQuery<OaOverDueReport> sqlPageQuery = new SqlPageQuery<OaOverDueReport>(OaOverDueReport.class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<OaOverDueReport> pageResult = oaDao.findDataWithPage(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				OaOverDueReport oaOverDue = pageResult.getData().get(i);
				O2oOverdueReport overDueReport = new O2oOverdueReport();
				overDueReport.setBankrefundcode(oaOverDue.getBankrefundcode());
				overDueReport.setBillcode(oaOverDue.getBankrefundcode());
				overDueReport.setCreatetime(oaOverDue.getCreatetime());
				overDueReport.setCreator(oaOverDue.getCreator());
				overDueReport.setDefaultamount(oaOverDue.getDefaultamount());
				overDueReport.setDefaultcapital(oaOverDue.getDefaultcapital());
				overDueReport.setDefaultdatenum(oaOverDue.getDefaultdatenum());
				overDueReport.setDefaultinterest(oaOverDue.getDefaultamount());
				overDueReport.setDisposalamoun(oaOverDue.getDefaultamount());
				overDueReport.setDisposaldate(oaOverDue.getCreatetime());
				overDueReport.setIdcard(oaOverDue.getIdcard());
				overDueReport.setIsdel(oaOverDue.getIsdel());
				overDueReport.setIsnew(oaOverDue.getIsnew());
				overDueReport.setLastupdatetime(oaOverDue.getLastupdatetime());
				overDueReport.setLastupdateuser(oaOverDue.getLastupdateuser());
				overDueReport.setLoaddate(oaOverDue.getLoaddate());
				overDueReport.setLoanamount(oaOverDue.getLoanamount());
				overDueReport.setLoanapplyno(oaOverDue.getLoanapplyno());
				overDueReport.setLoanername(oaOverDue.getLoanername());
				overDueReport.setLoannumber(oaOverDue.getLoannumber());
				overDueReport.setStatus(oaOverDue.getStatus());
				overDueReport.setRemarks(oaOverDue.getRemark());
				overDueReport.setProvice(oaOverDue.getProvice());
				overDueReport.setPenaltyinterest(oaOverDue.getPenaltyinterest());
				overDueReport.setMaturitydate(oaOverDue.getMaturitydate());
				overDueReport.setLtd(oaOverDue.getLtd());
				try {
					int a = serviceDao.insert(overDueReport);
					if (a > 0) {
						count++;
					}
				} catch (Exception e) {
					logger.info("同步历史逾期数据异常：[{}]", e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("插入历史逾期条数:[{}]", count);
	}

	/**
	 * 同步部门
	 */
	public void sycnHrOrgan() {
		logger.info("开始同步员工部门");
		String employeeSql = "SELECT t.node_id as nodeid FROM act_org_user u LEFT JOIN o2o_employee t ON u.user_no=t.employee_no where u.is_remove='0' GROUP BY u.user_no";
		int index = 0;
		int count = 0;
		while (index >= 0) {
			SqlPageQuery<HrEmployee> sqlPageQuery = new SqlPageQuery<HrEmployee>(HrEmployee.class, Constant.PROCESS_DATA_LIMIT, index, employeeSql);
			PageResult<HrEmployee> pageResult = workFlowDao.findDataWithPage(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				HrEmployee employee = pageResult.getData().get(i);
				String oaSql = "select * from eadb.hr_organ t where t.isdel='N' and t.nodeno=?";
				HrOrgan hrOrgan = oaDao.findEntity(oaSql, HrOrgan.class, employee.getNodeid());
				if (hrOrgan != null) {
					try {
						HrOrgan organ = workFlowDao.getById(HrOrgan.class, hrOrgan.getNodeno());
						if (organ != null) {
							workFlowDao.updateById(hrOrgan);
						} else {
							workFlowDao.insert(hrOrgan);
						}
						count++;
					} catch (Exception e) {
						logger.info("同步部门到工作流异常：[{}]", e.getMessage());
					}
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("结束同步部门更新条数[{}]", count);
	}

}
